package com.wink_172.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.wink_172.commons.R;
import com.wink_172.commons.databinding.ItemView15Binding;
import com.wink_172.commons.databinding.ItemView16Binding;
import com.wink_172.library.adapter.HomeListAdapter;
import com.wink_172.library.model.BaseDataItem;
import com.wink_172.library.model.json.FileData;
import com.wink_172.library.utils.CommonUtils;
import com.wink_172.library.utils.FileUtil;
import com.wink_172.library.utils.MMKVTool;
import com.wink_172.model.Constants;

import org.xutils.http.RequestParams;
import org.xutils.image.ImageOptions;
import org.xutils.x;

import java.io.File;
import java.util.List;


public class HBomeListAdapter extends HomeListAdapter {

    public HBomeListAdapter() {
    }

    public HBomeListAdapter(Context context, List<Object> dataList, RecyclerView listview) {
        super(context, dataList, listview);
    }

//    @Override
//    public HomeListAdapter.BaseViewsHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//        HomeListAdapter.BaseViewsHolder viewVolder = null;
//        return viewVolder;
//    }

    public BaseViewsHolder createCustomViewHolder(ViewGroup parent, int viewType) {
        View view = null;

        switch (viewType) {


            case com.wink_172.model.Constants.SCENE_DOWN_LOAD_LIST2:
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_view15, parent, false);
                return new ThumbListViewsHolder(view);
            case Constants.SCENE_DOWN_LOAD_STATUS_LIST:
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_view16, parent, false);
                return new List16ViewsHolder(view);


            default:
                return null;
        }
    }

    private class ThumbListViewsHolder extends BaseViewsHolder {

        private ItemView15Binding viewBinding;
        private ImageOptions options;

        public ThumbListViewsHolder(View itemView) {
            super(itemView);
            viewBinding = DataBindingUtil.bind(itemView);

            ImageOptions.ParamsBuilder paramsBuilder = new ImageOptions.ParamsBuilder() {
                @Override
                public RequestParams buildParams(RequestParams params, ImageOptions options) {
                    params.setCacheSize(1024 * 1024 * 30);
                    return params;
                }
            };
            options = new ImageOptions.Builder()
                    .setUseMemCache(true)
                    .setFadeIn(true)
                    .setImageScaleType(ImageView.ScaleType.FIT_XY)
//                    .setParamsBuilder(paramsBuilder)
                    .build();
        }

        @Override
        public void updateContent(Object itemData) {

            BaseDataItem item = (BaseDataItem) itemData;
            FileData single_file = (FileData) item.getData();


            if (isListSelect()) {
                viewBinding.select.setVisibility(View.VISIBLE);
                if (item.getSelect()) {
                    viewBinding.select.setActivated(true);

                } else {
                    viewBinding.select.setActivated(false);
                }
            } else {
                viewBinding.select.setVisibility(View.GONE);
            }

            viewBinding.text.setText(FileUtil.Companion.getFileNameWithSuffix(single_file.web_url));
            if (FileUtil.Companion.isCacheFileExist(single_file.local_url)) {
                Log.e(TAG, "updateContent: ====>>已下载:" + single_file.local_url);
                x.image().bind(viewBinding.image, single_file.local_url, options);
                viewBinding.hasDown.setVisibility(View.VISIBLE);
            } else {
//                Log.e(TAG, "updateContent: ====>>未下载:"+single_file.web_url );
                viewBinding.image.setImageResource(R.mipmap.icon_transparent);
                viewBinding.hasDown.setVisibility(View.GONE);
            }

        }
    }

    private class List16ViewsHolder extends BaseViewsHolder {

        private ItemView16Binding viewBinding;

        public List16ViewsHolder(View itemView) {
            super(itemView);
            viewBinding = DataBindingUtil.bind(itemView);


        }

        @Override
        public void updateContent(Object itemData) {

            BaseDataItem item = (BaseDataItem) itemData;
            viewBinding.text.setText(item.getContent());

            int num = CommonUtils.INSTANCE.getRandomNum(0, 7);

            switch (num) {
                case 0:
                    viewBinding.text.setBackgroundColor(x.app().getResources().getColor(R.color.color_pan1));
                    break;
                case 1:
                    viewBinding.text.setBackgroundColor(x.app().getResources().getColor(R.color.color_pan5));
                    break;
                case 2:
                    viewBinding.text.setBackgroundColor(x.app().getResources().getColor(R.color.color_pan9));
                    break;
                case 3:
                    viewBinding.text.setBackgroundColor(x.app().getResources().getColor(R.color.color_pan2));
                    break;
                case 4:
                    viewBinding.text.setBackgroundColor(x.app().getResources().getColor(R.color.logcat_level_verbose_color));
                    break;
                case 5:
                    viewBinding.text.setBackgroundColor(x.app().getResources().getColor(R.color.color_pan8));
                    break;
                case 6:
                    viewBinding.text.setBackgroundColor(x.app().getResources().getColor(R.color.logcat_level_info_color));
                    break;
                default:
                    viewBinding.text.setBackgroundColor(x.app().getResources().getColor(R.color.logcat_level_other_color));
                    break;

            }


        }
    }

    private static final String TAG = "HBomeListAdapter";


}